/**
 * Copyright (c) 2019 xstgongyi.org
 * All rights reserved.
 */
package org.xstgongyi.eduaid.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xstgongyi.eduaid.common.ControllerHelper;
import org.xstgongyi.eduaid.dataobject.BankAccountDO;
import org.xstgongyi.eduaid.manager.BankAccountManager;

/**
 * @author oldcrane<br/>
 * @created 2019-04-22
 */
@Controller
@RequestMapping("/bank-accounts")
public class BankAccountController {

	@Autowired
	private BankAccountManager accountManager;

	@GetMapping("/entry")
	public String entryPage(ModelMap map) {
		map.put("rows", accountManager.listAll());
		return "masterdata/bank_account";
	}

	@GetMapping("")
	@ResponseBody
	public Map<String, Object> list(@RequestParam(name = "id", required = false) Integer id) {
		List<BankAccountDO> accounts = accountManager.listAll();
		return ControllerHelper.successResponse(accounts);
	}

	@PostMapping("")
	@ResponseBody
	public Map<String, Object> add(@RequestBody BankAccountDO account) {
		if (null == account) {
			return (ControllerHelper.errorResponse(1, "非法输入：要添加的银行账户参数为空对象。"));
		}
		accountManager.insert(account);
		return ControllerHelper.successResponse(null);
	}

	@GetMapping("/{id}")
	@ResponseBody
	public Map<String, Object> get(@PathVariable("id") Integer id) {
		BankAccountDO account = accountManager.getById(id);
		if (null != account) {
			return ControllerHelper.successResponse(account);
		} else {
			return ControllerHelper.errorResponse(404, "Bank account does not exist(id:" + id + ").");
		}
	}

	@PostMapping("/{id}")
	@ResponseBody
	public Map<String, Object> update(@PathVariable("id") Integer id, @RequestBody BankAccountDO account) {
		if (null == account) {
			return (ControllerHelper.errorResponse(1, "非法输入：要添加的银行账户参数为空对象。"));
		}
		accountManager.update(account);
		return ControllerHelper.successResponse(null);
	}

}
